package com.qtyx.nms.ui.activity

import android.annotation.SuppressLint
import android.util.DisplayMetrics
import android.widget.EditText
import com.qtyx.nms.R
import com.qtyx.nms.mvp.model.bean.LoginBean
import com.qtyx.nms.mvp.model.network.RetrofitUtil
import com.qtyx.nms.mvp.presenter.LoginPresenter
import com.qtyx.nms.mvp.view.LoginView
import com.qtyx.nms.ui.base.BaseMVPActivity
import com.qtyx.nms.util.shared.AppInfoShared
import kotlinx.android.synthetic.main.ac_login.*

/**
 * 登录
 */
@SuppressLint("SetTextI18n")
class LoginActivity : BaseMVPActivity<LoginPresenter>(),LoginView{
    override fun getLayoutId()= R.layout.ac_login
    override fun createPresenter()=LoginPresenter(this)


    override fun initData() {
        isShowBack=false
        //记录账号
        if(AppInfoShared.getUserAccount().isNotEmpty()){
            etLoginAccount.setText(AppInfoShared.getUserAccount())
        }
        //记录密码
        if(AppInfoShared.getUserAccount().isNotEmpty()){
            etLoginPassword.setText(AppInfoShared.getUserPassword())
        }

        btnLoginSubmit.setOnClickListener {
            presenter.login()
        }

        val appMetrics: DisplayMetrics = this.resources.displayMetrics
        tvLoginMetricsInfo.text=""+
                "widthPixels：${appMetrics.widthPixels}\n"+
                "heightPixels：${appMetrics.heightPixels}\n"+
                "density：${appMetrics.density}\n"+
                "densityDpi：${appMetrics.densityDpi}\n"+
                "scaledDensity：${appMetrics.scaledDensity}\n"
    }

    override fun getAccount(): EditText =etLoginAccount
    override fun getPassword(): EditText=etLoginPassword
    override fun showVerifyHint(hint: String) {
        showToast(hint)
    }

    override fun loginSucceed(data:LoginBean) {
        //登录成功，保存数据
        AppInfoShared.saveUserAccount(etLoginAccount.text.toString())
        AppInfoShared.saveUserPassword(etLoginPassword.text.toString())
        AppInfoShared.saveToken(data.token)
        AppInfoShared.saveHospitalName(data.systemName)
        AppInfoShared.saveDeptId(data.deptId.toString())
        AppInfoShared.saveDeptName(data.deptName)
        AppInfoShared.saveServerDate(data.date)

        //设置为null，重新设置公共头部
        RetrofitUtil.setInstance(null)
        //跳转主页
        startActivityAndFinish(HomePageActivity::class.java)
    }
}